﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class admin_News_Big : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AdminCs.CheckLoginState();
        if (!IsPostBack)
        {
            bind();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }
    //删除 
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (Session["AdminType"].ToString() == "0")
        {
            SqlConnection conn = Class1.DBconnection();
            conn.Open();
            string sqlstr = "delete from News_Big where Big_Id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            SqlCommand sqlcom = new SqlCommand(sqlstr, conn);
            sqlcom.ExecuteNonQuery();

            SqlCommand cmd2 = new SqlCommand("delete from News where n_Big_ID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'", conn);
            cmd2.ExecuteNonQuery();

            conn.Close();
            bind();
        }
        else
        {
            Response.Write("<script language=javascript>alert('您不是超级管理员，删除失败！');window.location='News_Big.aspx';</script>");
        }

    }

    //更新 
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string strNews = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim();
        string strSort = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
        string strId = GridView1.DataKeys[e.RowIndex].Value.ToString();
        SqlConnection conn = Class1.DBconnection();
        conn.Open();
        string sqlstr = "update News_Big set News_Big='" + strNews + "',Sort=" + strSort + " where Big_Id='" + strId + "'"; 
        SqlCommand sqlcom = new SqlCommand(sqlstr, conn);
        sqlcom.ExecuteNonQuery();
        conn.Close();
        GridView1.EditIndex = -1;
        bind();
    }
    //取消 
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
    //绑定 
    public void bind()
    {
        string sqlstr = "select * from News_Big order by Sort asc";
        SqlConnection conn = Class1.DBconnection();
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "News_Big");
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "Big_Id" };//主键 
        GridView1.DataBind();

        PagedDataSource objPds = new PagedDataSource();
        objPds.DataSource = ds.Tables[0].DefaultView;
        objPds.AllowPaging = true;
        objPds.PageSize = 17;
        int CurPage;
        if (Request.QueryString["Page"] != null)
            CurPage = Convert.ToInt32(Request.QueryString["Page"]);
        else
            CurPage = 1;
        objPds.CurrentPageIndex = CurPage - 1;
        lblCurrentPage.Text = "当前页：第" + CurPage.ToString() + "页";

        if (!objPds.IsFirstPage)
            lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
        if (!objPds.IsLastPage)
            lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
        GridView1.DataSource = objPds;
        GridView1.DataBind();
        conn.Close();
    }

    protected void btnBig_Click(object sender, EventArgs e)
    {
        string strNewsBig = txtNewsBig.Text.Trim();
        if (strNewsBig == "")
        {
            Response.Write("<script>alert('标题不能为空！');history.go(-1);</script>");
        }
        else
        {
            SqlConnection conn = Class1.DBconnection();
            conn.Open();
            int Sort = 0;
            string strsql = "insert into News_Big(News_Big,Sort)values('" + strNewsBig + "','" + Sort + "')";
            SqlCommand cmd = new SqlCommand(strsql, conn);
            cmd.ExecuteNonQuery();
            Response.Write("<script language=javascript>alert('添加成功！');window.location='News_Big.aspx';</script>");
            conn.Close();
        }
    }
  
}
